/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.*;
import entities.*;
import java.util.ArrayList;

/**
 *
 * @author lion
 */
public class MessageDao {

    private Connection conn = null;
    private Statement stm = null;
    private ResultSet rs = null;
    private PreparedStatement ps = null;
    MysqlHelper mysql = new MysqlHelper();

    public void sendMessage(int adminid, String message) throws Exception {
        try {
            conn = mysql.GetConnection();
            ps = conn.prepareStatement("INSERT INTO fl_message VALUES (DEFAULT,?,?)");
            ps.setInt(1, adminid);
            ps.setString(2, message);
            ps.executeUpdate();
        } catch (Exception e) {
            throw e;
        }
    }

    public ArrayList<Message> getMessage() throws Exception {
        ArrayList<Message> messages = new ArrayList<Message>();
        try {
            conn = mysql.GetConnection();
            rs = mysql.ExecuteReader("SELECT * FROM fl_message");
            rs.last();
            Message message = new Message();
            message.setAdminid(rs.getInt("adminid"));
            message.setMessage(rs.getString("message"));
            messages.add(message);
        } catch (Exception e) {
            throw e;
        }
        return messages;
    }
}
